#!/usr/bin/perl -w
use strict;
use Getopt::Long;
my $exec_dir = "/net/cpp-group/Leo/bin/";
my $exec_file = $exec_dir . "matrix_extract";
#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888
my $usage = <<"USAGE";

USAGE:

    unit_test.pl
		
		This script tests the programme in the directory

USAGE

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory


# optional parameters
my $help = undef;
my $verbose = undef;
GetOptions('help' => \$help, 'verbose' => \$verbose);

die $usage if ($help);
my $verbose_str = $verbose ? "--verbose" : "";

sub prepare_cmd
{
	#remove beginning white space and new lines
	$_[0] =~tr/\t\n/ /d;
	$_[0] =~ s/\s+/ /gm;
	if ($_[1])
	{
		print STDERR $_[0], "\n";
	}
}

sub run_cmd
{
	prepare_cmd(@_);
	system $_[0] and die "Failed [ $_[0] ]\n$!\n";
}

sub test_diff
{
	my $cmd = "diff -s $_[0] $_[1]";
	my $diff_res = `$cmd`;
	#print STDERR $diff_res, "\n";
	die "Error:\n\t$_[0] and $_[1] are different\n" unless ($diff_res =~ /are identical/);
	print STDERR "\t\tIdentical: $_[0] and $_[1]\n";
}


my $cmd;

	print STDERR "\ttree upgma in binary format...\n";
	$cmd = <<"EXEC_END";
		tree_upgma
			--matrix_output
			--output_format b
			--header
			--verbose
			< unit_test.pairs
			> test.upgma.b.nh
EXEC_END
	run_cmd($cmd);

	print STDERR "\ttree upgma in pairs format...\n";
	$cmd = <<"EXEC_END";
		tree_upgma
			--matrix_output
			--output_format p
			--header
			--verbose
			< unit_test.pairs
			> test.upgma.p.nh
EXEC_END
	run_cmd($cmd);

	print STDERR "\textract sub trees in binary format...\n";
	$cmd = <<"EXEC_END";
		tree_extract_sub_trees_with_matrix
			--input_format b
			--tree_files_path test.subtrees.b.nh
			--sub_matrix_files_path test.b.matrix
			--output_format b

			--unchanged_tree_file test.unchanged.b.nh
			--branch_length_limit 1.0
			< test.upgma.b.nh
			> test.trunks.b.nh 
EXEC_END
	run_cmd($cmd);

	print STDERR "\textract sub trees in pairs format...\n";
	$cmd = <<"EXEC_END";
		tree_extract_sub_trees_with_matrix
			--input_format p
			--tree_files_path test.subtrees.p.nh
			--sub_matrix_files_path test.p.matrix
			--output_format p

			--unchanged_tree_file test.unchanged.p.nh
			--branch_length_limit 1.0
			< test.upgma.p.nh
			> test.trunks.p.nh 
EXEC_END
	run_cmd($cmd);

print STDERR "\tTest if binary and pairs output is identical\n";
test_diff("test.trunks.b.nh", "test.trunks.p.nh");
test_diff("test.unchanged.b.nh", "test.unchanged.p.nh");
test_diff("test.subtrees.b----1.nh", "test.subtrees.p----1.nh");
test_diff("test.subtrees.b----2.nh", "test.subtrees.p----2.nh");
test_diff("test.subtrees.b----3.nh", "test.subtrees.p----3.nh");
test_diff("test.subtrees.b----4.nh", "test.subtrees.p----4.nh");

for (my $i = 1; $i <= 4; ++$i)
{
	$cmd = <<"EXEC_END";
    matrix_extract
			--input_format b
			--output_format p
			--header
				< test.b----$i.matrix
				> test.b----$i.p.matrix
EXEC_END
	run_cmd($cmd);
	$cmd = <<"EXEC_END";
	matrix_extract
			--input_format p
			--output_format b
			--header
				< test.p----$i.matrix
				> test.p----$i.b.matrix
EXEC_END
	run_cmd($cmd);
#	round trip from binary will be different because of rounding
#	test_diff("test.p----$i.b.matrix", "test.b----$i.matrix");
	test_diff("test.b----$i.p.matrix", "test.p----$i.matrix");
}

print STDERR "\n", "*" x 80, "\n";
print STDERR "\n\tSuccess!!\n";
print STDERR "\n", "*" x 80, "\n";
unlink(glob("test*"));
